/**
* @description: 701. 二叉搜索树中的插入操作
* @author hewei
* @date 2022/8/12 19:17
* @version 1.0
*/

public class InsertIntoBST {

    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode temp = new TreeNode(val);
        if (root == null) return temp;
        TreeNode node = process(root, val);
        if (node.val > val) {
            node.left = temp;
        } else node.right = temp;
        return root;
    }

    public TreeNode process(TreeNode node, int target) {
        if (node.val < target) {
            if (node.right == null) return node;
            return process(node.right, target);
        } else {
            if (node.left == null) return node;
            return process(node.left, target);
        }
    }
}
